package _021_10_6

//暴力法
func removeOccurrences(s string, part string) string {
	strB := make([]byte, 0)
	for i := 0; i < len(s); i++ {
		strB = append(strB, s[i])
	}
	for {
		i, i2 := checkPart(strB, part)
		if i == -1 {
			result := ""
			for k := 0; k < len(strB); k++ {
				result += string(strB[k])
			}
			return result
		}
		//copy(strB[i:], strB[i2:])
		for z := i; z < i+len(strB[i2:]); z++ {
			strB[z] = strB[i2+z]
		}
		strB = strB[0 : len(strB)-len(part)]
	}
}

func checkPart(str []byte, part string) (int, int) {
	j := 0
	for i := 0; i < len(str) && len(str) >= len(part); i++ {
		if str[i] == part[0] && len(str[i:]) >= len(part) {
			for j = 1; j < len(part); j++ {
				if part[j] != str[i+j] {
					break
				}
			}
			if j == len(part) {
				return i, i + len(part)
			}
		}
	}
	return -1, -1
}

//第二种解法
func removeOccurrences2(s string, part string) string {
	result := make([]string, 0)
	j := 0
	for i := 0; i < len(s); i++ {
		result = append(result, string(s[i]))
		if len(result) >= len(part) {
			for j = 0; j < len(part); j++ {
				if result[len(result)-len(part)+j] != string(part[j]) {
					break
				}
			}
			if j == len(part) {
				result = result[0 : len(result)-len(part)]
			}
		}
	}

	ret := ""
	for i := 0; i < len(result); i++ {
		ret += result[i]
	}
	return ret
}
